package cn.edu.bjtu.net.temp;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.DynamicChannelBuffer;
import org.jboss.netty.channel.*;  
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.codec.string.StringDecoder;  
import org.jboss.netty.handler.codec.string.StringEncoder;  
  
import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.net.InetSocketAddress;  
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;  
  
/** 
 * God Bless You! 
 * Author: Fangniude 
 * Date: 2013-07-15 
 */  
public class NettyClient {  
  
    public static void main(String[] args) {  
        // Configure the client.  
        ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));  
  
        // Set up the default event pipeline.  
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {  
            @Override  
            public ChannelPipeline getPipeline() throws Exception {  
                return Channels.pipeline(new HttpRequestEncoder(), new HttpResponseDecoder(), new ClientHandler());  
            }  
        });  
  
        // Start the connection attempt.  
        ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", 9999));  
  
        // Wait until the connection is closed or the connection attempt fails.  
        future.getChannel().getCloseFuture().awaitUninterruptibly();  
  
        // Shut down thread pools to exit.  
        bootstrap.releaseExternalResources();  
    }  
  
    private static class ClientHandler extends SimpleChannelHandler {  
  
        @Override  
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {  
            if (e.getMessage() instanceof String) {  
                String message = (String) e.getMessage();  
                System.out.println(message);  
  
            } else{
            	System.out.println(e.getMessage());
            }
  
            super.messageReceived(ctx, e);  
        }  
  
        @Override  
        public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {  
            System.out.println("已经与Server建立连接。。。。");  
            new Thread(()->{
            	try{
            		  while(true){
                      	HttpRequest hr = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,"fff/fff");
              			ChannelBuffer cb = new DynamicChannelBuffer(1000);
              			cb.writeBytes("ffff".getBytes());
              			hr.setContent(cb);
              			ctx.getChannel().write(hr);
              			TimeUnit.SECONDS.sleep(1);
                      }
            	}catch(Exception e1){
            		e1.printStackTrace();
            	}
            }).start();
          
            
        }  
        
    }  
} 